﻿Public Class perSetores
    Inherits perPadrao

#Region "Declaração de variáveis"
    Private objBuscaId As New perProximoId
#End Region

#Region "Propriedades"

    Public ReadOnly Property sqlConsulta() As String
        Get
            Dim sSql As String
            sSql = "Select IDSetor, Setor From DC_Setores"
            Return sSql
        End Get
    End Property

#End Region

#Region "Métodos"

    Public Function inserirSetores(ByVal sSetor As String, _
                                   ByVal bRealizaVisitas As Boolean, _
                                   ByVal bRealizaContatos As Boolean) As Int32

        Dim iIdSetor As Int32
        Dim sSql As String

        Try
            iIdSetor = Me.objBuscaId.BuscaID("IDSetor", "DC_Setoreses")

            sSql = "  INSERT INTO DC_Setoreses "
            sSql &= " ( "
            sSql &= "   IDSetor, "
            sSql &= "   Setor,  "
            sSql &= "   RealizaVisitas, "
            sSql &= "   RealizaContatos "
            sSql &= " ) "
            sSql &= " VALUES "
            sSql &= "   ( @IDSetor, @Setor, @RealizaVisitas, @RealizaContatos ) "

            With MyBase.SQLCmd.Parameters
                .Clear()
                .AddWithValue("@IDSetor", iIdSetor)
                .AddWithValue("@Setor", sSetor)
                .AddWithValue("@RealizaVisitas", bRealizaVisitas)
                .AddWithValue("@RealizaContatos", bRealizaContatos)
            End With

            MyBase.executarAcao(sSql)

            Return iIdSetor
        Catch ex As Exception
            Throw New Exception("Exceção no método 'inserirSetores'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Function

    Public Sub alterarSetores(ByVal iIdSetor As Int32, _
                              ByVal sSetor As String, _
                              ByVal bRealizaVisitas As Boolean, _
                              ByVal bRealizaContatos As Boolean)

        Dim sSql As String

        Try

            sSql = "  UPDATE DC_Setoreses SET "
            sSql &= "   Setor = @Setor, "
            sSql &= "   RealizaVisitas = @RealizaVisitas, "
            sSql &= "   RealizaContatos = @RealizaContatos "
            sSql &= " WHERE "
            sSql &= "   IdSetor = @IDSetor "

            With MyBase.SQLCmd.Parameters
                .Clear()
                .AddWithValue("@Setor", sSetor)
                .AddWithValue("@RealizaVisitas", bRealizaVisitas)
                .AddWithValue("@RealizaContatos", bRealizaContatos)
                .AddWithValue("@IDSetor", iIdSetor)
            End With

            MyBase.executarAcao(sSql)

        Catch ex As Exception
            Throw New Exception("Exceção no método 'alterarSetores'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Sub

    Public Sub excluirSetores(ByVal iIdSetor As Int32)

        Dim sSql As String

        Try
            sSql = "  DELETE FROM "
            sSql &= "   DC_Setoreses "
            sSql &= " WHERE "
            sSql &= "   IdSetor = @IdSetor "

            With MyBase.SQLCmd.Parameters
                .Clear()
                .AddWithValue("@IdSetor", iIdSetor)
            End With

            MyBase.executarAcao(sSql)

        Catch ex As Exception
            Throw New Exception("Exceção no método 'excluirSetores'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Sub

    Public Function selecionarCampoSetores(ByVal sCampo As String, _
                                           ByVal iIDSetor As Long) As DataTable
        Dim sSql As String
        Dim dsDados As New DataSet

        Try
            sSql = " SELECT " & sCampo
            sSql &= " FROM DC_Setoreses "

            If (iIDSetor > 0) Then
                sSql &= " WHERE IDSetor = @IDSetor "
            End If

            With MyBase.SQLCmd.Parameters
                .Clear()
                .AddWithValue("@IDSetor", iIDSetor)
            End With

            dsDados = MyBase.executarConsulta(sSql, "DC_Setoreses")

            If Not dsDados Is Nothing AndAlso dsDados.Tables("DC_Setoreses").Rows.Count > 0 Then
                Return dsDados.Tables("DC_Setoreses")
            Else
                Return Nothing
            End If

        Catch ex As Exception
            Throw New Exception("Exceção no método 'selecionarCampoSetores'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Function

    Public Function selecionarSetores(ByVal iIDSetor As Int32) As DataRow

        Dim sSql As String
        Dim dsDados As New DataSet

        Try
            sSql = " SELECT * FROM DC_Setoreses "
            sSql &= " WHERE IDSetor = @IDSetor"

            With MyBase.SQLCmd.Parameters
                .Clear()
                .AddWithValue("@IDSetor", iIDSetor)
            End With

            dsDados = MyBase.executarConsulta(sSql, "DC_Setoreses")

            If Not dsDados Is Nothing AndAlso dsDados.Tables("DC_Setoreses").Rows.Count > 0 Then
                Return dsDados.Tables("DC_Setoreses").Rows(0)
            Else
                Return Nothing
            End If

        Catch ex As Exception
            Throw New Exception("Exceção no método 'selecionarSetores'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Function

    Public Function selecionarSetor(ByVal iIDSetor As Int32) As DataTable

        Dim sSql As String
        Dim dsDados As New DataSet

        Try
            sSql = " SELECT * FROM DC_Setores "

            If iIDSetor > 0 Then
                sSql &= " WHERE IDSetor = @IDSetor"
            End If

            With MyBase.SQLCmd.Parameters
                .Clear()
                If iIDSetor > 0 Then
                    .AddWithValue("@IDSetor", iIDSetor)
                End If
            End With

            dsDados = MyBase.executarConsulta(sSql, "DC_Setoreses")

            If Not dsDados Is Nothing AndAlso dsDados.Tables("DC_Setoreses").Rows.Count > 0 Then
                Return dsDados.Tables("DC_Setoreses")
            Else
                Return Nothing
            End If

        Catch ex As Exception
            Throw New Exception("Exceção no método 'selecionarSetores'. " & Environment.NewLine & Environment.NewLine & ex.Message)
        End Try

    End Function
#End Region
End Class
